package com.gzc.just.play.last.war.balanceserver.handler;

import com.gzc.just.play.last.war.common.annotation.MessageHandler;
import com.gzc.just.play.last.war.common.handler.SimpleMessageHandler;
import com.gzc.just.play.last.war.common.net.NetUtils;
import com.gzc.just.play.last.war.protobuf.bs.bstogc.BSToGC;
import com.gzc.just.play.last.war.protobuf.gc.gctobs.GCToBS;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * Balance Server Login Handler
 * Handles client login requests and provides Gate Server address
 */
@Component
@MessageHandler(name = "BalanceLoginHandler", serverTypes = MessageHandler.ServerType.BALANCE_SERVER)
public class BalanceLoginHandler implements SimpleMessageHandler {
    private static final Logger logger = LoggerFactory.getLogger(BalanceLoginHandler.class);
    
    // MsgNums
    private static final int ASK_LOGIN = 32770; // eMsgToBSFromGC_OneClinetLogin
    private static final int LOGIN_CHECK_RET = 204; // eMsgToGCFromBS_OneClinetLoginCheckRet
    private static final int ASK_GATE_ADDR_RET = 203; // eMsgToGCFromBS_AskGateAddressRet

    @Override
    public void handle(Object message, ChannelHandlerContext ctx) throws Exception {
        GCToBS.OneClinetLogin loginMsg = null;
        if (message instanceof byte[]) {
            loginMsg = GCToBS.OneClinetLogin.parseFrom((byte[]) message);
        } else if (message instanceof GCToBS.OneClinetLogin) {
            loginMsg = (GCToBS.OneClinetLogin) message;
        } else {
             logger.error("Unexpected message type: {}", message.getClass().getName());
             return;
        }
        
        if (loginMsg != null) {
             logger.info("Received login from user: {}", loginMsg.getUin());
             
             // 1. Send LoginCheckRet (204)
             BSToGC.ClinetLoginCheckRet checkRet = BSToGC.ClinetLoginCheckRet.newBuilder()
                 .setMgsid(BSToGC.MsgID.eMsgToGCFromBS_OneClinetLoginCheckRet)
                 .setLoginSuccess(1)
                 .build();
             NetUtils.sendMsg(ctx.channel(), LOGIN_CHECK_RET, checkRet);
             
             // 2. Send AskGateAddressRet (203)
             // In a real implementation, this would query a ServiceRegistry or LoadBalancer
             BSToGC.AskGateAddressRet gateRet = BSToGC.AskGateAddressRet.newBuilder()
                 .setMgsid(BSToGC.MsgID.eMsgToGCFromBS_AskGateAddressRet)
                 .setGateclient(1) 
                 .setToken("mock_gate_token_" + System.currentTimeMillis())
                 .setUserName(loginMsg.getUin())
                 .setIp("127.0.0.1")
                 .setPort(40001) // Default Gate Port
                 .build();
             NetUtils.sendMsg(ctx.channel(), ASK_GATE_ADDR_RET, gateRet);
             
             logger.info("Sent Gate Server info to user: {}", loginMsg.getUin());
        }
    }

    @Override
    public int getMessageId() {
        return ASK_LOGIN;
    }
    
    @Override
    public String getName() {
        return "BalanceLoginHandler";
    }

    @Override
    public Logger getLogger() {
        return logger;
    }
}